package com.aiguigu.stack;

public class Stack {
    public static void main(String[] args) {
       String expression ="7*2*1-5+1-5+3-4";
       ArrayStack numberStack = new ArrayStack(10);
       ArrayStack operatorStack = new ArrayStack(10);
       int index = 0;
       int res = 0;
       int number1 =0;
       int number2 = 0;
       char operator =0;
       char ch = ' ';//将上次扫描得到的char保存到ch
        String keepNum="";//用于拼接多位数
        while (true){
            ch = expression.substring(index,index+1).charAt(0);
            if (operatorStack.isOperator(ch)){
                if (!operatorStack.isEmpty()){
                    if (operatorStack.Priority(ch)<=operatorStack.Priority(operatorStack.peek())){
                        number1 = numberStack.pop();
                        number2 = numberStack.pop();
                        operator =(char) operatorStack.pop();
                        res = operatorStack.cal(number1,number2,operator);
                        numberStack.push(res);
                        operatorStack.push(ch);
                    }else {
                        operatorStack.push(ch);
                    }
                }else{
                    operatorStack.push(ch);
                }
            }else {
               keepNum+=ch;
               if (index == expression.length()-1){
                   numberStack.push(Integer.parseInt(keepNum));
               }else {
                   if (operatorStack.isOperator(expression.substring(index+1,index+2).charAt(0))){
                       numberStack.push(Integer.parseInt(keepNum));
                       keepNum = "";
                   }
               }
            }
            index++;
            if (index>=expression.length()){
                break;
            }

        }
        while(true){
           if (operatorStack.isEmpty()){
               break;
           }
           number1 =numberStack.pop();
           number2 = numberStack.pop();
           operator =(char) operatorStack.pop();
           res = numberStack.cal(number1,number2,operator);
           numberStack.push(res);
        }
        int res2 = numberStack.pop();
        System.out.println(res);
    }
}
class ArrayStack{
    private int maxSize;
    private int [] stack;
    private int top = -1;

    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[maxSize];
    }

    public int peek(){
        return stack[top];
    }

    public boolean isFull(){
        return top == maxSize-1;
    }

    public boolean isEmpty(){
        return top ==-1;
    }

    public void push(int value){
        if (isFull()){
            return;
        }
        top++;
        stack[top]=value;
    }

    public int  pop(){
        if (isEmpty()){
           throw new RuntimeException("栈空.....");
        }
        int a = stack[top];
        top--;
        return a;
    }

    public int Priority(int oper){
        if (oper == '*'||oper =='/'){
            return 1;
        }else if (oper == '+'|| oper =='-'){
            return 0;
        }else
            return -1;

    }

    public Boolean isOperator(char value){
        return value =='+'||value=='-'||value == '*' || value == '/';
    }

    public int cal(int num1,int num2,char oper){
        int res = 0;
        switch (oper){
            case '+':
                res = num1+num2;
                break;
            case '-':
                res = num2 - num1;
                break;
            case '*':
                res = num2*num1;
                break;
            case '/':
                res = num2/num1;
                break;
            default:
                break;
        }
        return res;
    }
}
